package com.xzzz.irda.guardcat.server.metric.physical;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.xzzz.common.base.util.okhttp.HttpUtil;
import com.xzzz.common.base.util.json.JsonUtil;
import com.xzzz.irda.guardcat.core.constants.GuardcatConstant;
import com.xzzz.irda.guardcat.server.app.pojo.MachineInfo;
import com.xzzz.irda.guardcat.server.metric.MetricFetchJob;
import com.xzzz.irda.guardcat.server.metric.MetricRepository;
import com.xzzz.irda.guardcat.server.metric.Repository;
import com.xzzz.irda.guardcat.server.repos.redis.PhysicalRepositoryInRedis;
import lombok.extern.slf4j.Slf4j;

/**
 * @author xzzz
 */
@Slf4j
@Repository(repo = PhysicalRepositoryInRedis.class, enabled = false)
public class PhysicalFetchJob extends MetricFetchJob<PhysicalInfo> {

    public PhysicalFetchJob(MachineInfo machineInfo, MetricRepository<PhysicalInfo> metricRepository) {
        super(machineInfo, metricRepository);
    }

    @Override
    public void run() {
        try {
            String body = HttpUtil.get(buildUrl(GuardcatConstant.API.METRIC_MACHINE));
            if (StrUtil.isNotBlank(body)) {
                PhysicalInfo physicalInfo = JsonUtil.toObj(body, PhysicalInfo.class);
                physicalInfo.setId(UUID.fastUUID().toString(true).toUpperCase());
                this.saveAndPublish(physicalInfo);
            }
        } catch (Exception e) {
            log.error("拉取 PHYSICAL_MACHINE 失败: " + e.getMessage());
        }
    }
}
